package kizzo.game 
{
	import away3d.cameras.Camera3D;
	import away3d.events.Object3DEvent;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Vector3D;
	import org.flashdevelop.utils.FlashConnect;
	/**
	 * ...
	 * @author kizzo
	 */
	public class CameraTweener extends Sprite
	{
		private var camera:Camera3D;
		
		private var start_position:Vector3D;
		private var end_position:Vector3D;
		private var look_at_position:Vector3D;
		
		private var current_step:int;
		private var num_steps:int;
		
		public static const TWEEN_COMPLETE:String = "TWEEN_COMPLETE";
		
		public function CameraTweener(camera:Camera3D) 
		{
			this.camera = camera;
		}
		
		
		public function tween_to(position:Vector3D, num_steps:int, look_at_position:Vector3D):void
		{
			this.current_step = 0;
			this.num_steps = Math.max(1, num_steps);
			
			this.start_position = new Vector3D(camera.position.x, camera.position.y, camera.position.z);
			this.end_position = position;
			this.look_at_position = look_at_position;			
			
			Main.get_instance().addEventListener(Event.ENTER_FRAME, frame_hnd);
		}
		
		
		private function frame_hnd(e:Event):void
		{						
			current_step ++;
			
			var rel_pos:Number = (current_step) / (num_steps);
			
			camera.x = start_position.x + (end_position.x - start_position.x) * rel_pos;
			camera.y = start_position.y + (end_position.y - start_position.y) * rel_pos;
			camera.z = start_position.z + (end_position.z - start_position.z) * rel_pos;
					
			
			camera.lookAt(this.look_at_position);
			
			FlashConnect.trace(camera.position.toString() + " " + rel_pos);
			
			if (current_step >= num_steps)
			{
				FlashConnect.trace("end");
				Main.get_instance().removeEventListener(Event.ENTER_FRAME, frame_hnd);
				this.dispatchEvent(new Event(TWEEN_COMPLETE));
			}
		}
		
	}

}